package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer"),
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto")
    })
    @ApiOperationSupport(includeParameters = "userDto.dataState,userDto.deptNo,userDto.email,userDto.postNo,userDto.realName,userDto.roleId")
    public ResponseResult page(@RequestBody UserDto userDto, @PathVariable Integer pageNum,@PathVariable Integer pageSize){
        PageResponse response = userService.page(userDto,pageNum,pageSize);
        return ResponseResult.success(response);
    }

    @PutMapping
    @ApiOperation("用户添加")
    public ResponseResult addUser(@RequestBody UserDto userDto) throws CloneNotSupportedException {
        userService.addUser(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("用户修改")
    public ResponseResult update(@RequestBody UserDto userDto) throws CloneNotSupportedException {
        userService.update(userDto);
        return ResponseResult.success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用禁用用户")
    public ResponseResult updateStatus(@PathVariable Long id,@PathVariable String status){
        userService.updateStatus(id,status);
        return ResponseResult.success();
    }

    @PostMapping("/list")
    @ApiOperation("用户列表")
    public ResponseResult getByName(@RequestBody String name){
        UserVo userVo = userService.getByName(name);
        return ResponseResult.success(userVo);
    }

    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult delete(@PathVariable Long userIds){
        userService.delete(userIds);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("密码重置")
    public ResponseResult resetPassword(@PathVariable Long userId){
        userService.resetPassword(userId);
        return ResponseResult.success();
    }
}
